package com.tjetc.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tjetc.common.JsonResult;
import com.tjetc.dao.ElderInfoMapper;
import com.tjetc.dao.EmployeeMapper;
import com.tjetc.dao.OrderInfoMapper;
import com.tjetc.dao.OutEnterMapper;
import com.tjetc.entity.ElderInfo;
import com.tjetc.entity.Employee;
import com.tjetc.entity.OrderInfo;
import com.tjetc.entity.OutEnter;
import com.tjetc.param.OutEnterCount;
import com.tjetc.service.OutEnterService;
import io.swagger.v3.core.util.Json;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
@Service
public class OutEnterServiceImpl implements OutEnterService {
    @Autowired
    private OutEnterMapper outEnterMapper;
    @Autowired
    private ElderInfoMapper elderInfoMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public JsonResult findById(Long id) {
        OutEnter outEnter=outEnterMapper.selectById(id);
        ElderInfo elderInfo=elderInfoMapper.selectById(outEnter.getElderlyId());
        Employee employee=employeeMapper.selectById(outEnter.getApproverId());
        outEnter.setElderly(elderInfo);
        outEnter.setEmployee(employee);
        return JsonResult.success(outEnter);
    }
    @Override
    public   JsonResult findElderPage(String name, Integer pageNo, Integer pageSize){
        //设置页码和每页数量
        Page<OutEnter> page=Page.of(pageNo,pageSize);
        //把adminPage传入mapper查询中，adminPage就是最后的分页结果
        Page<OutEnter>outEnterPage= outEnterMapper.selectPageLikeElder(page,name);
        for (OutEnter outEnter : outEnterPage.getRecords()) {
            ElderInfo elderInfo=elderInfoMapper.selectById(outEnter.getElderlyId());
            Employee employee=employeeMapper.selectById(outEnter.getApproverId());
            outEnter.setElderly(elderInfo);
            outEnter.setEmployee(employee);
        }
        //convert方法转换Page中 records集合元素类型 Admin---->AdminView
        //new Function<Admin,AdminView>(){};表示创建一个实现Function的匿名类 对象
        return JsonResult.success(outEnterPage);
    }
    @Override
    public   JsonResult findEmployeePage(String name, Integer pageNo, Integer pageSize){
        //设置页码和每页数量
        Page<OutEnter> page=Page.of(pageNo,pageSize);
        //把adminPage传入mapper查询中，adminPage就是最后的分页结果
        Page<OutEnter>outEnterPage= outEnterMapper.selectPageLikeEmployee(page,name);
        for (OutEnter outEnter : outEnterPage.getRecords()) {
            ElderInfo elderInfo=elderInfoMapper.selectById(outEnter.getElderlyId());
            Employee employee=employeeMapper.selectById(outEnter.getApproverId());
            outEnter.setElderly(elderInfo);
            outEnter.setEmployee(employee);
        }
        //convert方法转换Page中 records集合元素类型 Admin---->AdminView
        //new Function<Admin,AdminView>(){};表示创建一个实现Function的匿名类 对象
        return JsonResult.success(outEnterPage);
    }
    @Override
    public JsonResult deleteById(Long id) {
        int rows=outEnterMapper.deleteById(id);
        if (rows>0){
            return JsonResult.success("删除成功");
        }
        else {
            return JsonResult.fail("删除失败");
        }
    }
    //@Transactional用在service方法上，保证事务
    //注意：默认情况@Transactional只对运行时异常起作用(RuntimeException)
    @Transactional
    @Override
    public JsonResult save(OutEnter outEnterParam) {
        OutEnter outEnter=null;
        LocalDateTime now=LocalDateTime.now();
        //通过判断id值有无判断是新增还是更新，id值无是新增，id值有就是更新
        if(outEnterParam.getRecordId()==null||outEnterParam.getRecordId()<=0){
            //新增
            outEnter=new OutEnter();
            BeanUtils.copyProperties(outEnterParam,outEnter);
            outEnter.setCreateTime(now);
            outEnter.setUpdateTime(now);
            outEnterMapper.insert(outEnter);
            return JsonResult.success("新增成功",null);
        }else{
            //更新
            //根据id查询Admin信息
            outEnter=outEnterMapper.selectById(outEnterParam.getRecordId());
            if(outEnter==null){
                return JsonResult.fail("用户不存在，更新失败");
            }
            //要更新的字段
            outEnter.setUpdateTime(now);
            outEnter.setElderly(outEnterParam.getElderly());
            outEnter.setEmployee(outEnterParam.getEmployee());
            outEnter.setOutTime(outEnterParam.getOutTime());
            outEnter.setApplyTime(outEnterParam.getApplyTime());
            outEnter.setApproveRemark(outEnterParam.getApproveRemark());
            outEnter.setElderlyId(outEnterParam.getElderlyId());
            outEnter.setApproverId(outEnterParam.getApproverId() );
            outEnter.setInTime(outEnterParam.getInTime());
            outEnter.setIsDeleted(outEnterParam.getIsDeleted());
            outEnter.setReason(outEnterParam.getReason());
            outEnter.setStatus(outEnterParam.getStatus());
            outEnter.setApproveTime(outEnterParam.getApproveTime());
            int rows=outEnterMapper.updateById(outEnter);
            if (rows>0){
                return JsonResult.success("更新成功",null);
            }else {
                return JsonResult.fail("用户不存在，更新失败");
            }
        }
    }

    @Override
    public JsonResult selectRecordCount() {
        OutEnterCount outEnterCount=new OutEnterCount();
        outEnterCount.setOrderGoOut(outEnterMapper.selectOrderGoOut());
        outEnterCount.setOrderApplying(outEnterMapper.selectOrderApplying());
        outEnterCount.setOrderApproval(outEnterMapper.selectOrderApproval());
        outEnterCount.setOrderBackMonth(outEnterMapper.selectOrderBackMonth());
        return JsonResult.success(outEnterCount);
    }
}
